from tkinter import *
from tkinter import ttk
"""
魏大王学编程（www.weidawang.xyz）
tkinter 实用教程系列

Treeview 事件案例
"""
def onSelect(e):
    itm = tree.set(tree.focus())
    print(itm)
    pass

main = Tk()
data = [(1, "小明", 23, '男', '2021-09-21'), (2, "小强", 23, '男', '2021-09-21'),
        (3, "小红", 23, '女', '2021-09-21'), (4, "铁头", 23, '男', '2021-09-21')]
tree = ttk.Treeview(main, columns=('id', 'name', 'age', 'sex',
                    'birth'), show="headings", displaycolumns="#all")
tree.heading('id', text="编号", anchor=W)
tree.heading('name', text="姓名", anchor=W)
tree.heading('age', text="年龄", anchor=W)
tree.heading('sex',text="性别",anchor=W)
tree.heading('birth', text="出生日期", anchor=W)
for itm in data:
    tree.insert("",END,values=itm)
tree.pack(expand=1, fill=BOTH)

tree.bind("<<TreeviewSelect>>",onSelect)
main.mainloop()
